package com.eeepay.eeepay_v2.activity;

import android.widget.LinearLayout;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.LeftRightText;

import java.util.concurrent.TimeUnit;

import cn.eeepay.api.grpc.nano.MerchantSuperSearch;
import cn.eeepay.api.grpc.nano.MerchantSuperSearchServiceGrpc;
import io.grpc.ManagedChannel;

/**
 * Created by Ching on 2016/3/16.
 */
public class MerchantDetailsActivity extends ABBaseActivity {

    private LinearLayout ll_bp, ll_terminalInfo;
    private String merchantNo;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_merchant_details;
    }

    @Override
    protected void initWidget() {
        if(bundle != null){
            merchantNo = bundle.getString(Constant.MERCHANT_NO);
        }
        ll_bp = getViewById(R.id.ll_bp);
        ll_terminalInfo = getViewById(R.id.ll_terminal_info);
    }

    @Override
    protected void eventOnClick() {
        sendHttpRequest(0);
    }

    @Override
    protected void sendHttpRequest(int taskId) {
        showProgressDialog();
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, taskId, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) { MerchantSuperSearchServiceGrpc.MerchantSuperSearchServiceBlockingStub stub =
                    MerchantSuperSearchServiceGrpc.newBlockingStub(channel)
                            .withDeadlineAfter(Constant.grpc_time_out, TimeUnit.SECONDS);
                MerchantSuperSearch.MerchantSuperInfoRequest request = new MerchantSuperSearch.MerchantSuperInfoRequest();
                request.merchantNo = merchantNo;
                MerchantSuperSearch.MerchantSuperInfoResponse response = stub.merchantSuperMerInfo(request);
                return response;
            }

            @Override
            public void updateUI(Object result, int taskId) {
                dismissProgressDialog();
                if(result == null){
                    showToast("查询失败");
                    return;
                }
                MerchantSuperSearch.MerchantSuperInfoResponse response = (MerchantSuperSearch.MerchantSuperInfoResponse) result;
                if(!response.success){
                    showToast(response.msg);
                    return;
                }
                MerchantSuperSearch.MerchantSuperInfoSear info = response.merchantInfo;
                setRightText(R.id.lrt_merchant_details_no, info.merchantNo);
                setRightText(R.id.lrt_merchant_details_name, info.merchantName);
                setRightText(R.id.lrt_merchant_details_phone, info.mobilephone);
                //0：商户关闭；1：正常；2 冻结
                if("0".equals(info.status)){
                    setRightText(R.id.lrt_merchant_details_state, "商户关闭", R.color.red);
                }else if("1".equals(info.status)){
                    setRightText(R.id.lrt_merchant_details_state, "正常", R.color.green);
                }else if("2".equals(info.status)){
                    setRightText(R.id.lrt_merchant_details_state, "冻结", R.color.red);
                }
                MerchantSuperSearch.SuperBpInfo[] superBpInfo = response.superBpInfo;
                for(int i = 0; i < superBpInfo.length; i++){
                    LeftRightText lt = new LeftRightText(mContext);
                    lt.setLeftText(superBpInfo[i].bpName);
                    // 1待一审  2待平台审核  3审核失败  4正常  0关闭
                    if("1".equals(superBpInfo[i].bpStatus)){
                        lt.setRightTextColor("待一审", R.color.red);
                    }else if("2".equals(superBpInfo[i].bpStatus)){
                        lt.setRightTextColor("待平台审核", R.color.red);
                    }else if("3".equals(superBpInfo[i].bpStatus)){
                        lt.setRightTextColor("审核失败", R.color.red);
                    }else if("4".equals(superBpInfo[i].bpStatus)){
                        lt.setRightTextColor("正常", R.color.unify_grounding_white);
                    }
                    ll_bp.addView(lt);
                }
                MerchantSuperSearch.SuperTerminalInfo[] terminalInfo = response.superTerminalInfo;
                for(int i = 0; i < terminalInfo.length; i++){
                    LeftRightText lt = new LeftRightText(mContext);
                    lt.setLeftText(terminalInfo[i].typeName);
                    lt.setRightLeftText(terminalInfo[i].sN);
                    String status = "1".equals(terminalInfo[i].openStatus) ? "已分配" : "已使用";
                    lt.setRightText(status);//开通状态1为已分配2为已使用
                    ll_terminalInfo.addView(lt);
                }

                setRightText(R.id.lrt_merchant_details_tradeSum, response.totalamount + "元");
                setRightText(R.id.lrt_merchant_details_tradeCount, response.totalcount + "笔");
            }
        });
    }
}
